package com.kindac.together;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.PopupWindow;
import android.widget.ToggleButton;

import com.kindac.together.common.Kin;
import com.kindac.together.db.KinDB;
import com.kindac.together.uitl.Utils;
import com.kindac.together.widget.Together;

/**
 * 启动闪频
 * 
 * @author kindac
 */
public class SplashScreen extends Activity {

    /** Called when the activity is first created. */
    private PopupWindow             pw;
    // private TextView text;
    private ToggleButton            wordsBtn;
    private ToggleButton            musicBtn;

    private int                     time             = Kin.timer;

    private Handler                 mHandler         = new Handler();

    private boolean                 isClick          = false;

    private OnCheckedChangeListener onChangeListener = new OnCheckedChangeListener() {

                                                         @Override
                                                         public void onCheckedChanged(CompoundButton buttonView,
                                                                                      boolean isChecked) {
                                                             time = Kin.timer;
                                                             switch (buttonView.getId()) {
                                                                 case R.id.toggleButton1:
                                                                     Kin.isWrite = isChecked;
                                                                     break;
                                                                 case R.id.toggleButton2:
                                                                     Kin.isMusic = isChecked;
                                                                     break;
                                                             }
                                                         }

                                                     };

    final Runnable                  timer            = new Runnable() {

                                                         public void run() {

                                                             if (time-- >= 0) {
                                                                 // text.setText(String.valueOf(time--));
                                                                 mHandler.postDelayed(this, 1000);
                                                             } else {
                                                                 // text.setVisibility(View.INVISIBLE);

                                                                 if (pw != null) {
                                                                     pw.dismiss();
                                                                     pw = null;
                                                                 }

                                                                 // 加载图片
                                                                 try {
                                                                     Utils.init(SplashScreen.this);
                                                                 } catch (Exception e) {
                                                                     e.printStackTrace();
                                                                 }
                                                                 Intent viewIntent = new Intent(SplashScreen.this,
                                                                                                Together.class);
                                                                 startActivity(viewIntent);
                                                                 overridePendingTransition(R.anim.fade, R.anim.hold);
                                                                 finish();

                                                             }
                                                         }
                                                     };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        Log.d(SplashScreen.class.getSimpleName(), "## create SplashScreen now");

        disPlay();
        // Intent viewIntent = new Intent(SplashScreen.this, LolActivity.class);
        // startActivity(viewIntent);
        // finish();
        SplashInit();
    }

    /**
     * 判断屏幕大小
     */
    private void disPlay() {
        Display display = getWindowManager().getDefaultDisplay();

        if (Kin.mWidth > display.getWidth()) Kin.disPlay = Kin.mini;
        if (Kin.mWidth < display.getWidth()) Kin.disPlay = Kin.large;

        Kin.mWidth = display.getWidth();
        Kin.mHeight = display.getHeight();
    }

    private void SplashInit() {
        // text = (TextView) findViewById(R.id.timerText);
        mHandler.postDelayed(new Runnable() {

            public void run() {
                mHandler.postDelayed(timer, 0);
            }
        }, 500);
    }

    private void SplashInit2() {

        mHandler.postDelayed(new Runnable() {

            public void run() {
                pop();
                mHandler.postDelayed(timer, 0);
            }
        }, 500);

    }

    private void pop() {
        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        final View popView = mLayoutInflater.inflate(R.layout.pop, null);
        wordsBtn = (ToggleButton) popView.findViewById(R.id.toggleButton1);
        wordsBtn.setOnCheckedChangeListener(onChangeListener);
        musicBtn = (ToggleButton) popView.findViewById(R.id.toggleButton2);
        musicBtn.setOnCheckedChangeListener(onChangeListener);
        if (pw == null || !pw.isShowing()) {
            pw = new PopupWindow(popView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
            //
            // 这个 和点击 退出有关系....
            //
            pw.setBackgroundDrawable(new BitmapDrawable());
            pw.setAnimationStyle(R.style.PopupAnimation);
            pw.showAtLocation(findViewById(R.id.mainLL), Gravity.CENTER | Gravity.CENTER, 0, 0);
        } else pw.dismiss();

        // TODO: 根据不同屏幕 不同坐标
        switch (Kin.disPlay) {

            case Kin.mini:

                break;
            case Kin.defalut:

                break;
            case Kin.large:

                break;
            default:
                break;
        }
        pw.update(Kin.mWidth, Kin.mHeight, Kin.mWidth / 6, Kin.mHeight / 3);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (isClick == false) {
            // 你找到第一个彩蛋了
            KinDB db = KinDB.getInstance(this);
            db.updateConfigs(this, 1, 0, 0, 0, -1);
            isClick = true;
            mHandler.removeCallbacks(timer);
            time = Kin.timer * 2;
            SplashInit2();
        }
        return true;
    }
}
